package tmt.usercenter.web.domain;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.tmt.annotation.LazyLoadDefine;
import com.tmt.annotation.LazyLoadMapping;
import com.tmt.annotation.Select2OptionValue;
import com.tmt.jpa.jpaservice.SyncUpdate;
import com.tmt.jpafieldvalueconvert.JpaFieldValueConvertListener;
import com.voodoodyne.jackson.jsog.JSOGGenerator;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.io.Serializable;
import java.util.List;

@Getter
@Setter
@Entity
@JsonIgnoreProperties(value = {"hibernateLazyInitializer", "handler", "fieldHandler"})
@EntityListeners({JpaFieldValueConvertListener.class})
@JsonIdentityInfo(generator = JSOGGenerator.class)

@LazyLoadDefine({
        @LazyLoadMapping(name = "1", value = {"originSystem"})
})
public class TmtUser extends UserInfo implements Serializable {

    /**
     * 用户所属的客户系统。
     */
    @Select2OptionValue("originSystem.id")
    @ManyToOne(cascade = {CascadeType.REFRESH}, fetch = FetchType.LAZY)
    @JoinColumn(name = "originSystem_id")
    private ClientSystem originSystem;

    /**
     * 用户创建的客户系统
     */
    @OneToMany(mappedBy = "creator", cascade = {CascadeType.REFRESH})
    private List<ClientSystem> clientSystems;

    @SyncUpdate
    @Select2OptionValue("userRoles[].role.id")
    @OneToMany(mappedBy = "user", cascade = {CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.REFRESH})
    private List<TmtUserRole> userRoles;

    /**
     * 用户绑定的第三方帐号。
     */
    @OneToMany(mappedBy = "tmtUser", cascade = {CascadeType.REFRESH})
    private List<TmtOauthAccount> tmtOauthAccounts;


    /**
     * 用户对客户端的授权列表。
     */
    @OneToMany(mappedBy = "tmtUser", cascade = {CascadeType.REFRESH})
    private List<TmtApproval> tmtApprovals;

    public TmtUser() {
    }


    public TmtUser(long userId) {
        this.setId(userId);
    }

}
